<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="图书名称" prop="bookname">
        <el-input
            v-model="queryParams.bookname"
            placeholder="请输入图书名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="图书类别" prop="booktype">
        <el-select
            v-model="queryParams.booktype"
            placeholder="请选择图书类别"
            clearable
            style="width: 200px"
        >
          <el-option
              v-for="item in bookTypeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['book:bookinfo:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['book:bookinfo:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['book:bookinfo:remove']"
        >删除
        </el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['book:bookinfo:export']"
        >导出
        </el-button>
      </el-col>-->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bookinfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="编号" align="center" prop="id"/>-->
      <el-table-column label="图书编号" align="center" prop="bookid"/>
      <el-table-column label="图书名称" align="center" prop="bookname"/>
      <el-table-column label="出版社" align="center" prop="publisher"/>
      <el-table-column label="作者" align="center" prop="author"/>
      <el-table-column label="图书类别" align="center" prop="booktype" :formatter="formatBookType"/>
      <el-table-column label="剩余数量" align="center" prop="remain"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['book:bookinfo:edit']">修改
          </el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['book:bookinfo:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改图书信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="bookinfoRef" :model="form" :rules="rules" label-width="80px">
        <!--        <el-form-item label="图书编号" prop="bookid">
                  <el-input v-model="form.bookid" placeholder="请输入图书编号"/>
                </el-form-item>-->
        <el-form-item label="图书名称" prop="bookname">
          <el-input v-model="form.bookname" placeholder="请输入图书名称" :disabled="isEditMode"/>
        </el-form-item>
        <el-form-item label="出版社" prop="publisher">
          <el-input v-model="form.publisher" placeholder="请输入出版社"/>
        </el-form-item>
        <el-form-item label="作者" prop="author">
          <el-input v-model="form.author" placeholder="请输入作者" :disabled="isEditMode"/>
        </el-form-item>
        <el-form-item label="图书类别" prop="booktype">
          <el-select v-model="form.booktype" placeholder="请选择图书类别">
            <el-option
                v-for="item in bookTypeOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="剩余数量" prop="remain">
          <el-input-number v-model="form.remain" :min="0" :max="1000" placeholder="数量"/>
        </el-form-item>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Bookinfo">
import {listBookinfo, getBookinfo, delBookinfo, addBookinfo, updateBookinfo} from "@/api/book/bookinfo";

const {proxy} = getCurrentInstance();

const bookinfoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const isEditMode = ref(false);
const formInline = reactive({
  user: '',
  region: '',
  date: '',
})
const bookTypeOptions = ref([
  {value: 1, label: '文学类'},
  {value: 2, label: '科技类'},
  {value: 3, label: '艺术类'},
  {value: 4, label: '教育类'},
  {value: 5, label: '其他'}
]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    bookid: null,
    bookname: null,
    publisher: null,
    author: null,
    booktype: null,
    remain: null
  },
  rules: {
    publisher: [
      {required: true, message: '出版社不能为空', trigger: 'blur'}
    ],
    author: [
      {required: true, message: '作者不能为空', trigger: 'blur'}
    ],
    bookname: [
      {required: true, message: '图书名称不能为空', trigger: 'blur'}
    ],
    remain: [
      {required: true, message: '剩余数量不能为空', trigger: 'blur'}
    ],
    booktype: [
      {required: true, message: '请选择图书类别', trigger: 'change'}
    ]
  }
});

const {queryParams, form, rules} = toRefs(data);

function formatBookType(row, column, cellValue) {
  const bookTypeMap = {
    1: '文学类',
    2: '科技类',
    3: '艺术类',
    4: '教育类',
    5: '其他'
  };
  return bookTypeMap[cellValue] || '未知类别';
}

/** 查询图书信息列表 */
function getList() {
  loading.value = true;
  listBookinfo(queryParams.value).then(response => {
    bookinfoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    bookid: null,
    bookname: null,
    publisher: null,
    author: null,
    booktype: null,
    remain: null
  };
  proxy.resetForm("bookinfoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加图书信息";
  isEditMode.value = false; // 新增时禁用状态为 false
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getBookinfo(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改图书信息";
    isEditMode.value = true; // 修改时禁用状态为 true
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["bookinfoRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateBookinfo(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addBookinfo(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除图书信息编号为"' + _ids + '"的数据项？').then(function () {
    return delBookinfo(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('book/bookinfo/export', {
    ...queryParams.value
  }, `bookinfo_${new Date().getTime()}.xlsx`)
}

getList();
</script>
